import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Map from '@/Map/map.vue'
import main from '@/Map/main.vue'

import Login from '@/Map/Login.vue' //登录注册界面

import login from '@/Map/login/Login.vue' //登录模块
import register from '@/Map/login/Register.vue' //注册模块

import manager from '@/Map/Manager.vue' //管理员模块
import managerUser from '@/Map/manager/ManagerUser.vue'
import managerCar from '@/Map/manager/ManagerCar.vue'
import managerNotice from '@/Map/manager/ManagerNotice.vue' 
import announce from '@/Map/manager/Announce.vue'	// 发布公告
import allCarLoc from '@/Map/manager/allCarLoc.vue'
import routeSetting from '@/Map/manager/RouteSetting.vue'


import userInfo from '@/Map/info/UserInfo.vue'
import carInfo from '@/Map/info/CarInfo.vue'
import addCar from '@/Map/info/addCar.vue'

// 消息板块
import news from '@/Map/news/News.vue'
import showNotice from '@/Map/news/ShowNotice.vue'
import myNews from '@/Map/news/MyNews.vue'

//个人中心
import personal from '@/Map/user/Personal.vue'
import personalCenter from '@/Map/user/PersonalCenter.vue'

Vue.use(Router)

let LoginChildren = [{
		path: '/',
		redirect: '/login/login',
		component: login
	},
	{
		path: 'login',
		name: 'login',
		component: login
	},
	{
		path: 'register',
		name: 'register',
		component: register
	}
]

let NewsChildren = [{
		path: '/',
		redirect: '/main/news/myNews'
	},
	{
		path: 'showNotice',
		name: 'showNotice',
		component: showNotice
	}, {
		path: 'myNews',
		name: 'myNews',
		component: myNews
	}
]

let UserChildren = [{
	path: 'userinfo',
	name: 'userinfo',
	component: personalCenter,
	children: [{
		path: 'addCar',
		name: 'addCar',
		component: addCar
	}]
}]

let ManagerChildren = [{
		path: '/',
		redirect: '/main/manager/managerUser'
	},
	{
		path: 'managerUser',
		name: 'managerUser',
		component: managerUser
	},
	{
		path: 'managerCar',
		name: 'managerCar',
		component: managerCar
	},
	{
		path: 'managerNotice',
		name: 'managerNotice',
		component: managerNotice
	},
	{
		path: 'announce',
		name: 'announce',
		component: announce
	},
	{
		path: 'userInfo',
		name: 'userInfo',
		component: userInfo
	},
	{
		path: 'carInfo',
		name: 'carInfo',
		component: carInfo
	},
	{
		path: 'addCar',
		name: 'AddCar',
		component: addCar
	},
	{
		path: 'allCarLoc',
		name: 'allCarLoc',
		component: allCarLoc
	},
	{
		path: 'routeSetting',
		name: 'routeSetting',
		component: routeSetting
	}

]
const router = new Router({
	routes: [{
			path: '/',
			redirect: '/login'
		},
		{
			path: '/login',
			component: Login,
			children: LoginChildren
		},
		{
			path: '/main',
			component: main,
			children: [{
					path: '/',
					redirect: '/main/map',

				},
				{
					path: 'map',
					name: 'map',
					component: Map
				},
				{
					path: 'news',

					component: news,
					children: NewsChildren
				},

				{
					path: 'user',
					component: personal,
					children: UserChildren
				},

				{
					path: 'manager',
					component: manager,
					children: ManagerChildren
				}
			]
		},
	]
})


router.beforeEach((to, from, next) => {
	let isLogin = localStorage.getItem('user');
	let regx = /^\/login/;
	if (regx.test(to.path)) {
		if (isLogin != null) {
			next('/main')
		} else {
			next();

		}
	} else {
		if (isLogin == null) {
			next('/login/login')
		} else {
			next();
		}
	}
})
export default router
